package com.example.timer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.SystemClock
import android.view.WindowManager
import android.widget.Chronometer
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )      //设置全屏显示

        //设置计时器功能
        val chronometer = findViewById<Chronometer>(R.id.chronometer1)
        chronometer.base = SystemClock.elapsedRealtime()
        chronometer.format = "%s"
        chronometer.start()

        chronometer.setOnChronometerTickListener {
            //判断是否超过10秒，然后停止计时
            if (SystemClock.elapsedRealtime() - chronometer.base >= TimeUnit.SECONDS.toMillis(10)){
                //停止计时器
                chronometer.stop()
            }
        }

    }
}
